﻿Imports System.Data.OracleClient
'Imports System.Data.Odbc
Imports system.Data.OleDb

Public Class DataAccess

    Dim objSqlConnection As System.Data.OleDb.OleDbConnection
    Dim booEstaConectado As Boolean

    Public Sub New()
        Try
            objSqlConnection = New System.Data.OleDb.OleDbConnection
            objSqlConnection.ConnectionString = "Provider=OraOLEDB.Oracle.1;Data Source=XE;User ID=labadm;Password=labadm;Unicode=True"
            objSqlConnection.Open()
            booEstaConectado = True
        Catch ex As OleDb.OleDbException
            booEstaConectado = False
            Throw New Exception("No se pudo abrir la base de datos")
        Catch ex As Exception
            booEstaConectado = False
            Throw New Exception("No se pudo abrir la base de datos")
        End Try
    End Sub

    Public Function getDataReader(ByVal strSql As String) As OleDb.OleDbDataReader
        Dim datareader As OleDb.OleDbDataReader
        If booEstaConectado = False Then
            Throw New Exception("No se pudo abrir la base de datos")
            Return Nothing
        End If

        Try
            Dim objCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand
            objCommand.Connection = objSqlConnection
            objCommand.CommandText = strSql
            datareader = objCommand.ExecuteReader()
            Return datareader
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function ejecutaSQL(ByVal strSQL As String) As Integer
        If booEstaConectado = False Then
            Throw New Exception("No se pudo abrir la base de datos")
        End If
        Try
            Dim objCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand
            objCommand.Connection = objSqlConnection
            objCommand.CommandText = strSQL

            Return objCommand.ExecuteNonQuery
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function CloseConnection() As Boolean
        Try
            objSqlConnection.Close()
            Return True
        Catch ex As Exception
            Throw New Exception("No se pude cerrar la clase")
            Return False
        End Try
    End Function
End Class